package com.useeinfo.oa.modules.repair.biz;

import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.enums.ResultEnums;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.repair.dao.RepairLevelDao;
import com.useeinfo.oa.modules.repair.entity.RepairLevel;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Auther: xudong.sun
 * @Date: 2018/12/26 17:02
 * @Description:
 */
@Service("repairLevelBiz")
public class RepairLevelBiz extends CrudBiz<RepairLevelDao, RepairLevel> {

    @Autowired
    private HttpServletRequest httpServletRequest;

    public ResultDto repairLevel() {
        SystemUser systemUser = SessionKeyUtils.getSessionUser(httpServletRequest);
        Enterprise sessionEnterprise = SessionKeyUtils.getSessionEnterprise(httpServletRequest);
        if (systemUser == null || sessionEnterprise == null) {
            ResultUtil.error(ResultEnums.NO_LOGIN_MESSAGE);
        }
        QueryParam queryParam = new QueryParam(-1, -1);
        queryParam.setSqlOrder("order by id");
        queryParam.getSqlMap().put("enterpriseId", StringConverters.null2String(sessionEnterprise.getId()));
        return ResultUtil.success(findJSONList(queryParam));
    }

    /**
     * 查询维修等级
     *
     * @param grade 维修等级
     * @return
     */
    public RepairLevel getRepairLevel(String grade) {
        if (StringUtils.isBlank(grade)) {
            return null;
        }
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("grade", grade);
        List<RepairLevel> repairLevelList = this.findList(queryParam);
        if (CollectionUtils.isNotEmpty(repairLevelList)) {
            return repairLevelList.get(0);
        }
        return null;
    }
}
